package org.itzheng.myrxframe.http.network;

import android.util.Log;

import org.itzheng.and.baseutils.json.JsonHelper;
import org.itzheng.myrxframe.http.bean.DsTestBean;
import org.itzheng.myrxframe.http.bean.RowsBean;
import org.itzheng.myrxframe.http.bean.abase.BaseResult;
import org.itzheng.myrxframe.http.rxjava.IRxNet;
import org.itzheng.myrxframe.http.rxjava.MyObserver;
import org.itzheng.myrxframe.http.callback.IBindDataCallBack;
import org.itzheng.myrxframe.http.retrofit.api.TestApiService;
import org.itzheng.myrxframe.http.retrofit.utils.ApiServiceUtils;
import org.itzheng.myrxframe.http.rxjava.RxHelper;
import org.itzheng.myrxframe.http.rxjava.RxMap;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2019-4-9.
 */
public class NetDataTest {
    private static final String TAG = "NetDataTest";
    IRxNet mRxNet;
    Class<TestApiService> apiClazz = TestApiService.class;

    public NetDataTest(IRxNet iRxNet) {
        mRxNet = iRxNet;
    }

    public static NetDataTest newInstance(IRxNet iRxNet) {
        return new NetDataTest(iRxNet);
    }


    /**
     * 直接解析javabean
     *
     * @param page
     * @param size
     * @param iBindDataCallBack
     */
    public void getShareList3(int page, int size, final IBindDataCallBack<RowsBean> iBindDataCallBack) {
        ApiServiceUtils.create(apiClazz)
                .getShareListRxMapBean(
                        new RxMap().put("fType", 1).put("fPage", page).put("fSize", size)
                ).compose(RxHelper.observableIO2Main())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
                //                .doOnNext(new Consumer<BaseResult<RowsBean>>() {
//                    @Override
//                    public void accept(BaseResult<RowsBean> rowsBeanBaseResult) throws Exception {
//
//                    }
//                })
                .subscribe(new MyObserver<RowsBean>(mRxNet) {
                    @Override
                    public void onSuccess(List<RowsBean> rows) {
                        if (iBindDataCallBack != null) {
                            iBindDataCallBack.onSuccess(rows);
                        }
                    }

                    @Override
                    public void onError(String error) {
                        if (iBindDataCallBack != null) {
                            iBindDataCallBack.onError(error);
                        }
                    }
                });
    }

    /**
     * 在map解析javabean
     *
     * @param page
     * @param size
     * @param iBindDataCallBack
     */
    public void getShareList2(int page, int size, final IBindDataCallBack<RowsBean> iBindDataCallBack) {
        ApiServiceUtils.create(apiClazz)
                .getShareListRxMap(
                        new RxMap().put("fType", 1).put("fPage", page).put("fSize", size)
                )
                .map(new Function<ResponseBody, List<RowsBean>>() {
                    @Override
                    public List<RowsBean> apply(ResponseBody responseBody) throws Exception {
//                        DsTestBean testBean = new Gson().fromJson(responseBody.string(), DsTestBean.class);
                        DsTestBean testBean = JsonHelper.fromJson(responseBody.string(), DsTestBean.class);
                        return testBean.dsERP.rows;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<RowsBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (mRxNet != null) {
                            mRxNet.addDisposable(this, d);
                        }
                    }

                    @Override
                    public void onNext(List<RowsBean> value) {
                        if (iBindDataCallBack != null) {
                            iBindDataCallBack.onSuccess(value);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (iBindDataCallBack != null) {
                            iBindDataCallBack.onError(e.toString());
                        }
                    }

                    @Override
                    public void onComplete() {
                        if (mRxNet != null) {
                            mRxNet.removeDisposable(this);
                        }
                    }
                });
    }

    public void getShareList(int page, int size, IBindDataCallBack iBindDataCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("fType", 1);
            jsonObject.put("fPage", page);
            jsonObject.put("fSize", size);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(null, jsonObject.toString());
        ApiServiceUtils.create(apiClazz)
                .getShareListRxString(body)
                .map(new Function<ResponseBody, StringBuffer>() {
                    @Override
                    public StringBuffer apply(ResponseBody responseBody) throws Exception {
                        String s = responseBody.string();
                        Log.w(TAG, Thread.currentThread().getName() + "map apply: " + s);
                        return new StringBuffer(s);
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .doOnNext(new Consumer<StringBuffer>() {
                    @Override
                    public void accept(StringBuffer s) throws Exception {
                        Log.w(TAG, Thread.currentThread().getName() + "doOnNext: " + s);
                        s.reverse();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<StringBuffer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (mRxNet != null) {
                            mRxNet.addDisposable(this, d);
                        }
                        Log.w(TAG, Thread.currentThread().getName() + "onSubscribe: ");
                    }

                    @Override
                    public void onNext(StringBuffer value) {
                        Log.w(TAG, Thread.currentThread().getName() + "onNext: " + value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w(TAG, Thread.currentThread().getName() + "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        Log.w(TAG, Thread.currentThread().getName() + "onCompleted: ");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
